package com.wqmei.service;

import android.app.Service;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

import com.wqmei.controller.SearchActivity;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import java.util.function.IntToDoubleFunction;

/**
 * author: wqmei
 * date: 2018/4/23 19:59
 * description:
 */
public class MusicService extends Service implements MediaPlayer.OnBufferingUpdateListener,MediaPlayer.OnPreparedListener
{
    private MediaPlayer mediaPlayer = null;
    public final ServiceBinder serviceBinder = new ServiceBinder();

    private static final String TAG = "MusicService日志";

    //内部类,获取实例
    class ServiceBinder extends Binder
    {
        public MusicService getMusicService()
        {
            return MusicService.this;
        }
    }

    @Override
    public void onCreate()
    {
        super.onCreate();
        if (mediaPlayer != null)
        {
            mediaPlayer.reset();
            mediaPlayer.release();
            mediaPlayer = null;
        }
        mediaPlayer = new MediaPlayer();
        //设置为媒体音量
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mediaPlayer.setOnBufferingUpdateListener(MusicService.this);
        mediaPlayer.setOnPreparedListener(MusicService.this);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent)
    {
        String url = intent.getStringExtra("musicUrl");
        Log.i(TAG, url);

        new Thread(()->
        {
            try
            {
                mediaPlayer.reset();
                mediaPlayer.setDataSource(url);
                mediaPlayer.prepare();
                mediaPlayer.start();
            } catch (IOException e)
            {
                e.printStackTrace();
            }

            Intent returnIntent = new Intent();
            returnIntent.setAction("updateTime");
            Timer timer = new Timer();
            TimerTask timerTask = new TimerTask()
            {
                @Override
                public void run()
                {
                    returnIntent.putExtra("position", getCurrentPosition());
                    returnIntent.putExtra("totalTime", getTotalTime());
                    sendBroadcast(returnIntent);
                }
            };
            timer.schedule(timerTask,100,200);
        }).run();


        return serviceBinder;
    }

    @Override
    public void onBufferingUpdate(MediaPlayer mp, int percent)
    {

    }

    @Override
    public void onPrepared(MediaPlayer mp)
    {
        mp.start();
    }



    /**
     * 获取总时长
     * @return
     */
    public int getTotalTime()
    {
        return mediaPlayer.getDuration();
    }

    /**
     * 获取当前进度
     * @return
     */
    public int getCurrentPosition()
    {
        return mediaPlayer.getCurrentPosition();
    }

    /**
     * 销毁
     */
    @Override
    public void onDestroy()
    {
        super.onDestroy();
        mediaPlayer.stop();
    }

    /**
     * 暂停
     */
    public void pauseMusic()
    {
        mediaPlayer.pause();
    }

    /**
     * 播放
     */
    public void startMusic()
    {
        mediaPlayer.start();
    }

    /**
     * 播放状态
     * @return
     */
    public boolean isPlaying()
    {
        return mediaPlayer.isPlaying();
    }

}
